package com.langsin.Sort;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

/**
 * @Auther: MFM
 * @Date: 2021/7/22 - 07 - 22 - 19:32
 * @version: 1.0
 */

public class ShellSortDemo {
    public static void main(String[] args) {
        int[] arr = new int[10000000];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int) (Math.random() * 100000000);
        }

        Date date1 = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        String date1str = simpleDateFormat.format(date1);
        System.out.println("希尔排序前时间" + date1str);

        sort(arr);

        Date date2 = new Date();
        String date2str = simpleDateFormat.format(date2);
        System.out.println("希尔排序后时间" + date2str);
    }

    public static int[] sort(int[] sourceArray) {
        int[] arr = Arrays.copyOf(sourceArray, sourceArray.length);
        for (int gap = arr.length; gap > 0; gap = gap / 2) {
            for (int i = gap; i < arr.length; i++) {
                int tmp = arr[i];
                int j = i;
                while (j - gap >= 0 && tmp < arr[j - gap]) {

                    arr[j] = arr[j - gap];

                    j -= gap;
                }
                if (j != i) {
                    arr[j] = tmp;
                }
            }
        }

        return arr;
    }
}
